package com.erpanderp.wam.tm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.tm.model.entity.RecruitmentVendor;
import com.erpanderp.wam.util.HibernateUtil;

public class VendorRegistrationDAO {
	
	
	 public boolean isVendorCodeExistOrNot(String vendorCode) {
			boolean isVendorCodeExist = false;
			try {
				Session session = HibernateUtil.getSessionFactory()
						.getCurrentSession();
				session.getTransaction().begin();
				String queryString = "from RecruitmentVendor r where r.vendorCode=:vendorCode";
				Query query = session.createQuery(queryString).setString(
						"vendorCode", vendorCode);
				query.setCacheable(false);

				List tempList = query.list();

				if (tempList == null || tempList.size() == 0) {
					isVendorCodeExist = true;
				}

				session.close();
				return isVendorCodeExist;
			} catch (Exception e) {
				e.printStackTrace();
				return isVendorCodeExist;
			}
		}
	 
	 public String getLastGeneratedVendorCode(){
			
			String queryString="select r.vendorCode from RecruitmentVendor r ";
			
			System.out.println("QUERY: " + queryString);
			
			List<String> codeList = null;
			
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.getTransaction().begin();		
				Query query = session.createQuery(queryString);
				query.setCacheable(false);
				codeList = (List<String>) query.list();
				session.getTransaction().commit();
			}catch(HibernateException e){
				e.printStackTrace();
			}
			
			String lastCode = null;
			for (String code : codeList) {
				
				if (lastCode == null) {
					lastCode = code;
					continue;
					
				}else if (Long.parseLong(code.substring(11)) > Long.parseLong(lastCode.substring(11))) {
						lastCode = code;
						continue;
					
				}
				System.out.println(lastCode + "============ " + code);
			}		
			return lastCode;	
		}
	 
	 public RecruitmentVendor createRecruitmentVendor(RecruitmentVendor recruitmentVendor){
			try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();
			System.out.println("FLUSH MODE: " + session.getFlushMode());
			session.setFlushMode(FlushMode.COMMIT);
			session.save(recruitmentVendor);
			session.getTransaction().commit();
			return recruitmentVendor;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
		}
	 
	 public List<RecruitmentVendor> getRecruitmentVendors(){ 
		 List<RecruitmentVendor> vendors ;	
		
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentVendor r where r.status='ACTIVE' order by r.createDate desc";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false);
				vendors = (List<RecruitmentVendor>) query.list();
				System.out.println("SIZE OF vendors : "+vendors.size());
				session.getTransaction().commit();
				
				return vendors;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	 
	 public RecruitmentVendor updateRecruitmentVendor(RecruitmentVendor recruitmentVendor){
			try{
			 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			 session.beginTransaction();
			 session.setFlushMode(FlushMode.COMMIT);
			 session.saveOrUpdate(recruitmentVendor);
			 session.getTransaction().commit();
			 return recruitmentVendor;	
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
	 
	 /*public RecruitmentVendor getRecruitmentVendorByVendorId(Long vendorId) {
		 RecruitmentVendor recruitmentVendor = null;
				try {
					Session session = HibernateUtil.getSessionFactory().getCurrentSession();
					session.beginTransaction();
					session.setFlushMode(FlushMode.COMMIT);
					Query query = session.createQuery("from RecruitmentVendor rv where rv.recruitmentVendorId=:vendorId").setLong(
							"vendorId", vendorId);
					query.setCacheable(false);
					List<RecruitmentVendor> tempList = query.list();
					session.getTransaction().commit();
					
					if(tempList != null && tempList.size()>0){
						recruitmentVendor = tempList.get(0);
						return recruitmentVendor;
					}else{
						return null;
					}
				} catch (Exception e) {
					e.printStackTrace();
					return null;
				}		
			}*/
	 
	 public RecruitmentVendor getRecruitmentVendorByVendorId(Long vendorId){ 
		 RecruitmentVendor recruitmentVendor ;		
		 System.out.println("INSIDE DAO =========="+vendorId);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				String queryString = "from RecruitmentVendor rv where rv.recruitmentVendorId = :vendorId";
				
				Query query = session.createQuery(queryString);
				query.setCacheable(false).setLong("vendorId", vendorId);			
				recruitmentVendor = (RecruitmentVendor) query.uniqueResult();
				System.out.println("recruitmentVendor====Return Obj==="+recruitmentVendor);
				session.getTransaction().commit();
				
				return recruitmentVendor;
				
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}	
		}
	/* public RecruitmentVendor deleteRecruitmentVendor(Long vendorId)
	{
		 RecruitmentVendor recruitmentVendor = null;
			
		 System.out.println("INSIDE DELETE METHOD:::::::::::"+vendorId);
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				//session.setFlushMode(FlushMode.COMMIT);
				//System.out.println("ROWE COUNT===0000000000000000000");
			  //  Query query = session.createQuery("delete from RecruitmentVendor where vendorCode ="+ vendorCode);
			  ////  System.out.println("ROWE COUNT======1111111111111");
			  //  int row=query.executeUpdate();
				//System.out.println("ROWE COUNT=================="+row);
				String queryString = "update RecruitmentVendor r set status='INACTIVE'  where r.vendorCode=:vendorCode";
				recruitmentVendor = (RecruitmentVendor) session.load(RecruitmentVendor.class, vendorId);
				session.delete(recruitmentVendor);
				session.getTransaction().commit();
				//session.close();
				return recruitmentVendor;
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}		
	 }
	 */
	 public static void main(String a[]){
		// System.out.println(new VendorRegistrationDAO().deleteRecruitmentVendor(4l));
	 }

}
